import Vue from 'vue'
import App from './App'
import store from './store'
Vue.prototype.$store = store
Vue.config.productionTip = false

App.mpType = 'app'
// 就这两行即可
import uView from "@/uview";
Vue.use(uView);

Vue.mixin({
	onLoad() {
		this.$appInfo = {
			appid: 'wx9ac8eecf404e43f1',
			secret: 'a5b082cce4eee32e86ab43a46ba91229',
		}
	},
	methods: {
		async getuserinfo(res) {
			let _this = this
			let openid = await this.getOpenid();
			let userInfo = res.target.userInfo;
			userInfo.openid = openid;
			userInfo.type = 'edit'
			console.log(userInfo)
			uni.showLoading({
				title: '登陆中...'
			});
			uniCloud
				.callFunction({
					name: 'addUserInfo',
					data: userInfo
				})
				.then(res => {
					_this.$store.dispatch('setUserinfo', res.result)
					uni.hideLoading();
				})
				.catch(err => {
					uni.hideLoading();
					uni.showModal({
						content: `登录失败，请重试`,
						showCancel: false
					});
					console.error(err);
				});
		},
		getOpenid() {
			let _this = this;
			return new Promise(resolve => {
				uni.login({
					success: async function(loginRes) {
						console.log(loginRes)
						//#ifdef MP-WEIXIN
						let res = await uniCloud.callFunction({
							name: 'loginBycode',
							data: {
								type: 'wx',
								code: loginRes.code
							}
						})
						resolve(res.result);
						//#endif
						//#ifdef MP-QQ
						console.log(loginRes)
						let result = await uniCloud.callFunction({
							name: 'loginBycode',
							data: {
								type: 'qq',
								code: loginRes.code
							}
						})
						resolve(result.result);
						//#endif
					}
				});
			});
		},
		color16(index) {
			//十六进制颜色随机
			let r = Math.floor(Math.random() * 256);
			let g = Math.floor(Math.random() * 256);
			let b = Math.floor(Math.random() * 256);
			let color = '#' + r.toString(16) + g.toString(16) + b.toString(16);
			return color;
		},
	}
})
const app = new Vue({
	store,
	...App
})
app.$mount()
